package in.muton.edushare.pages.cp.backup.restore;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.panels.upload.uploadify.UploadifyProcessPage;

import java.io.File;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.util.upload.FileItem;

import com.edushare.common.dao.SystemBackupDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.SystemUtils;
import com.edushare.common.vo.SystemBackup;

public class ConfigUploadProcessPage extends UploadifyProcessPage {

	private SystemBackup systemBackup;
	private static Log log = LogFactory.getLog(ConfigUploadProcessPage.class);
	
	public ConfigUploadProcessPage(PageParameters params){
		super(params);
		
	}
	
	@Override
	public void processFile(FileItem fileItem) {
		String param = getParam("bkpId")[0];
		systemBackup = new SystemBackupDAO(EduShareSession.get().getSessionORMInterface()).getSystemBackupById(Integer.parseInt(param));
		String dir = SystemUtils.getAbsolutePathFromRelative(systemBackup.getDirectoryPath());
		log.debug("Path >>>>>>> "+dir);
		File folder = new File(dir);
		folder.mkdirs();
		File file = new File(folder, fileItem.getName());
		FileUpload fileUpload = new FileUpload(fileItem);
		try {
			fileUpload.writeTo(file);
		} catch (IOException e) {
			throw new EduShareRuntimeException("Error while uploading config file");
		}
	}

	public void setSystemBackup(SystemBackup systemBackup) {
		this.systemBackup = systemBackup;
	}

	public SystemBackup getSystemBackup() {
		return systemBackup;
	}
}
